import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'
// @ts-ignore
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:"/",
      component:()=>import("../layouts/Layout.vue"),
      children:[
        {
          path:"", name:"home", component:()=>import("../views/common/Home.vue")
        },
        {
          path:"article", name:"article-detail", component:()=>import("../views/article/ArticleDetail.vue")
        },
        {
          path:"article/:id", name:"article-View", component:()=>import("../views/article/ArticleView.vue")
        },
        {
          path:"category/:id", name:"category", component:()=>import("../views/common/Category.vue")
        },
        {
          path:"tag/:id", name:"tag", component:()=>import("../views/common/Tags.vue")
        },
        {
          path:"search", name:"search", component:()=>import("../views/common/Search.vue")
        },
        {
          path:"notifications", name:"notifications", component:()=>import("../views/community/Notification.vue"), meta: { requiresAuth: true }
        },
        {
          path:"articles", name:"article-list", component:()=>import("../views/article/ArticleList.vue")
        },
        {
          path:"editor", name:"article-editor", component:()=>import("../views/article/ArticleEditor.vue"), meta:{requiresAuth:true}
        },
        {
          path:"agent", name:"agent", component:()=>import("../views/common/agent.vue")
        }
      ]
    },
    {
      path:"/user",
      component:()=>import("../layouts/UserLayout.vue"),
      children:[
        {
          path:"/user/profile",
          name:"user-profile",
          component:()=>import("../views/user/UserProfile.vue")
        },
        {
          path:"/user/settings",
          name:"user-settings",
          component:()=>import("../views/user/UserSettings.vue"),
          meta: { requiresAuth: true }
        },
        {
          path:"/user/favorites",
          name:"user-favorites",
          component:()=>import("../views/user/UserFavorites.vue")
        },
        {
          path:"/user/likes",
          name:"user-likes",
          component:()=>import("../views/user/UserLikes.vue")
        },
        {
          path:"/user/articles",
          name:"user-articles",
          component:()=>import("../views/user/UserArticles.vue")
        },
        {
          path: '',
          redirect: '/user/profile'  // 👈 默认跳到个人资料
        }
      ]
    },
    {
      path:"/auth",
      component:()=>import("../layouts/AuthLayout.vue"),
      children:[
        {
          path:"login", name:"login", component:()=>import("../views/user/Login.vue")
        },
        {
          path:"register", name:"register", component:()=>import("../views/user/Register.vue")
        }
      ]
    },
    {
      path:"/admin",
      component:()=>import("../layouts/AdminLayout.vue"),
      meta:{requiresAuth: true, requiresAdmin: true},
      children:[
        {
          path:"dashboard", name:"admin-dashboard", component:()=>import("../views/admin/AdminDashboard.vue")
        },
        {
          path:"comments", name:"admin-comments", component:()=>import("../views/admin/AdminComment.vue")
        },
        {
          path:"users", name:"admin-users", component:()=>import("../views/admin/AdminUser.vue")
        },
        {
          path:"articles", name:"admin-articles", component:()=>import("../views/admin/AdminArticle.vue")
        },
        {
          path: '',
          redirect: '/admin/dashboard'
        }
      ]
    },
    {
      path:"/:catchAll(.*)", name:"not-found", component:()=>import("../views/common/NotFound.vue")
    }
  ]
})
// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  // 检查 token 是否过期
  if (userStore.token) {
    const isExpired = userStore.checkTokenExpiry()
    if (isExpired) {
      // token 已过期，清除用户信息并重定向到登录页
      userStore.logout()
      next('/auth/login')
      return
    }
  }
  next()
})
export default router
